# -*- coding: utf-8 -*-
from django.db import models
from django.forms import ModelForm, TextInput, DateInput, Textarea
import django.forms as forms
import re

class Server(models.Model):
	sn = models.CharField(u"SN服务编号",blank=True,max_length=50)
	state = models.CharField(u"状态",blank=True,max_length=50,default=u"waiting")
	eth0 = models.CharField(u"ip",blank=True,max_length=50,help_text=u"格式:1.1.1.1/255.255.255.0")
	eth1 = models.CharField(u"ip",blank=True,max_length=50,help_text=u"格式:1.1.1.1/255.255.255.0")
	hostname = models.CharField(u"机器名",blank=True,max_length=200)
	password = models.CharField(u"密码",blank=True,max_length=50)
	mac = models.CharField(u"mac地址",blank=True,max_length=100)
	state_code = models.IntegerField(u"状态代码,0为等待任务，1为执行任务",default=0,max_length=2)
	timestrap = models.DateTimeField(u"时间", auto_now=True)
	message = models.CharField(u"message消息",blank=True,default=u"等待")
	xmlrpc_id = models.IntegerField(u"所属机房id",blank=True)
	profile = models.CharField(u"选择安装的系统",blank=True)
	def __unicode__(self):
		return self.sn

class xmlrpc_api(models.Model):
	ip = models.IPAddressField(u"IP地址",blank=True)
	idc = models.CharField(u"IDC机房",blank=True)
	port = models.IntegerField(u"端口",blank=True)
	def __unicode__(self):
		return self.ip


class NewserverForms(forms.Form):
	eth0 = forms.CharField(required=False)
	eth1 = forms.CharField(required=False)
	hostname = forms.CharField(required=False)
	password = forms.CharField(required=False)
	cobbler_server_id = forms.IntegerField()
	profile = forms.CharField()
	mac = forms.CharField()

	def clean_eth0(self):
		eth0 = self.cleaned_data['eth0']
		if re.match(r'/d/./d/./d/./d/.///d/./d/./d/./d/.',eth0):
			raise forms.ValidationError(u"格式不正确！")
		return eth0
